//
//  RightCell.m
//  TimeLineDemo
//
//  Created by dllo on 16/5/19.
//  Copyright © 2016年 wjg. All rights reserved.
//

#import "RightCell.h"
#import "CellEditState.h"
#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height

@interface RightCell ()

@property (nonatomic, strong)UIView *lineView;

//@property (nonatomic, assign)BOOL isEdit;

@property (nonatomic, strong)CellEditState *cellEditState;

@property (nonatomic, strong)UIButton *editButton;

@end


@implementation RightCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createView];
        self.cellEditState = [CellEditState defaultEdit];
        //self.cellEditState.isEdit = NO;

    }
    return self;
}



- (void)createView {

    self.contentView.backgroundColor = [UIColor clearColor];
    
    
    self.deleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.deleteButton.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:self.deleteButton];
    self.deleteButton.alpha = 0;
    [self.deleteButton setImage:[UIImage imageNamed:@"delete"] forState:UIControlStateNormal];
    [self.deleteButton addTarget:self action:@selector(clickDeleteButton:) forControlEvents:UIControlEventTouchUpInside];
    
    self.editButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.editButton.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:self.editButton];
    self.editButton.alpha = 0;
    [self.editButton setImage:[UIImage imageNamed:@"add"] forState:UIControlStateNormal];
    [self.editButton addTarget:self action:@selector(clickEditButton:) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    
    self.picImageView = [[UIImageView alloc]init];
    self.picImageView.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:self.picImageView];
    self.picImageView.userInteractionEnabled = YES;
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]init];
    [self.picImageView addGestureRecognizer:tap];
    [tap addTarget:self action:@selector(clickPicimageView)];
    
    self.nameLabel = [[UILabel alloc]init];
    self.nameLabel.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:self.nameLabel];
    self.nameLabel.font = [UIFont systemFontOfSize:13];
    
    self.moneyLabel = [[UILabel alloc]init];
    self.moneyLabel.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:self.moneyLabel];
    self.moneyLabel.font = [UIFont systemFontOfSize:13];
    
    self.lineView = [[UIView alloc]init];
    self.lineView.backgroundColor = [UIColor lightGrayColor];
    [self.contentView addSubview:self.lineView];
    
    
    self.photoImageView = [[UIImageView alloc]init];
    self.photoImageView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:self.photoImageView];
    self.photoImageView.userInteractionEnabled = YES;
    self.photoImageView.layer.masksToBounds = YES;
    self.photoImageView.layer.cornerRadius = 5;

    
    self.remarksLabel = [[UILabel alloc]init];
    self.remarksLabel.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:self.remarksLabel];
    self.remarksLabel.textColor = [UIColor lightGrayColor];
    self.remarksLabel.font = [UIFont systemFontOfSize:13];
    

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(editChanged:) name:@"editChanged" object:nil];
    
    
}



- (void)layoutSubviews {
    [super layoutSubviews];
    
    self.deleteButton.frame = CGRectMake(WIDTH / 2 - 20, 25, 30, 30);
    self.editButton.frame = CGRectMake(WIDTH / 2 - 20, 25, 30, 30);
    self.picImageView.frame = CGRectMake(WIDTH / 2 - 20, 20, 40, 40);
    self.nameLabel.frame = CGRectMake((WIDTH / 2 - 20) + 50, 30, 40, 20);
    self.moneyLabel.frame = CGRectMake((WIDTH / 2 - 20) + 50 + 50 , 30, 100, 20);
    self.photoImageView.frame = CGRectMake((WIDTH / 2 - 20 - 50), 20, 40, 40);
    
    self.lineView.frame = CGRectMake(WIDTH / 2 , 0, 1, 20);
    if (self.remarksLabel.text != nil) {
        self.nameLabel.frame = CGRectMake((WIDTH / 2 - 20) + 50, 20, 40, 20);
        self.moneyLabel.frame = CGRectMake((WIDTH / 2 - 20) + 50 + 50 , 20, 100, 20);
        self.remarksLabel.frame = CGRectMake((WIDTH / 2 - 20) + 50, 35, 150, 20);
        
    }
    
}


- (void)editChanged:(NSNotification *)notification {
    self.cellEditState.isEdit = NO;
    [self closeEdit];
}




- (void)closeEdit {
    self.nameLabel.alpha = 1;
    self.moneyLabel.alpha = 1;
    self.photoImageView.alpha = 1;
    self.remarksLabel.alpha = 1;

    self.deleteButton.center = CGPointMake(WIDTH / 2, 40);
    self.editButton.center = CGPointMake(WIDTH / 2, 40);
    self.deleteButton.alpha = 0;
    self.editButton.alpha = 0;
}

- (void)openEdit {
    self.nameLabel.alpha = 0;
    self.moneyLabel.alpha = 0;
    self.photoImageView.alpha = 0;
    self.remarksLabel.alpha = 0;
    self.deleteButton.center = CGPointMake(WIDTH / 2 - 120, 40);
    self.editButton.center = CGPointMake(WIDTH / 2 + 120, 40);
    self.deleteButton.alpha = 1;
    self.editButton.alpha = 1;
}

- (void)clickPicimageView {

    NSLog(@"%d", self.cellEditState.isEdit);
    if (self.cellEditState.isEdit == NO) {
        [UIView animateWithDuration:0.5 animations:^{

            [self openEdit];
            self.cellEditState.isEdit = YES;

        }];
    }
    else {
        [UIView animateWithDuration:0.5 animations:^{
            [self closeEdit];

            [[NSNotificationCenter defaultCenter] postNotificationName:@"editChanged" object:nil];
        }];
        
    }

    
    
//    self.cellEditState.isEdit = !self.cellEditState.isEdit;
    //NSLog(@"%d", self.cellEditState.isEdit);

    
}

//- (void)setCellEditState:(CellEditState *)cellEditState {
//    if (_cellEditState.isEdit != cellEditState.isEdit) {
//        [[NSNotificationCenter defaultCenter] postNotificationName:@"editChanged" object:nil];
//        NSLog(@"qqqqq");
//    }
//    else {
//        NSLog(@"ffffff");
//    }
//}

- (void)setAccountBook:(Account *)accountBook {
    _accountBook = accountBook;
    self.picImageView.image = [UIImage imageNamed:accountBook.picName];
    self.nameLabel.text = accountBook.name;
    
    self.moneyLabel.text = [NSString stringWithFormat:@"%.2lf", [accountBook.money floatValue]];
    self.photoImageView.image = [UIImage imageWithData:accountBook.imageData];
    self.remarksLabel.text = accountBook.remarks;
}

- (void)clickDeleteButton:(UIButton *)button {
    [self.deleteDelegate rightCellDeleateCell:self.index section:self.section];
    self.cellEditState.isEdit = !self.cellEditState.isEdit;
    
}

- (void)clickEditButton:(UIButton *)button {
    NSLog(@"被点");
    
    [self.deleteDelegate rightEditCell:[NSString stringWithFormat:@"%@", self.accountBook.money] pic:self.accountBook.picName type:self.accountBook.type remarks:self.accountBook.remarks image:self.photoImageView.image  index:self.index section:self.section];
}


- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
